package com.prs.crm.action.sys;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.BaseEntity;
import com.prs.crm.domain.Currency;
import com.prs.crm.domain.District;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.Rival;
import com.prs.crm.domain.doc.DeptDoc;
import com.prs.crm.domain.doc.DeptDocType;
import com.prs.crm.domain.doc.DocType;
import com.prs.crm.domain.doc.Document;
import com.prs.crm.domain.doc.UserDoc;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.JobTitle;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Role;
import com.prs.crm.domain.hr.Salary;
import com.prs.crm.domain.hr.Train;
import com.prs.crm.domain.purchase.PaymentDetails;
import com.prs.crm.domain.purchase.PurchaseOrder;
import com.prs.crm.domain.purchase.PurchaseReturned;
import com.prs.crm.domain.sale.QuotationSheet;
import com.prs.crm.domain.sale.Returns;
import com.prs.crm.domain.sale.SalesContract;
import com.prs.crm.domain.sale.SalesReceives;
import com.prs.crm.domain.sale.SalesReceivesRegist;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.Purchase;
import com.prs.crm.domain.stock.Supplier;
import com.prs.crm.domain.store.StoreFlit;
import com.prs.crm.domain.store.StoreIn;
import com.prs.crm.domain.store.StoreInDetail;
import com.prs.crm.domain.store.StoreOut;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.service.stock.StoreInService;
import com.prs.crm.service.sys.CodeService;

@SuppressWarnings("unchecked")
public class CodeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Code code;
	private Supplier supplier;
	private DeptDoc deptDoc;
	private UserDoc userDoc;
	private Customer customer;
	private Rival rival;
	private Dept dept;
	private Person person;
	private Role role;
	private Salary salary;
	private Train train;
	private JobTitle jobTitle;
	private Currency currency;
	private District district;
	private Product product;
	private StoreRoom room;
	private DocType docType;
	private DeptDocType deptDocType;
	
	private  Document document;
	private Purchase purchase;

	private PurchaseOrder purchaseOrder;

	private PaymentDetails paymentDetail;

	private PurchaseReturned purchaseReturned;

	private SalesContract salesContract;

	private QuotationSheet quotationsheet;

	private SalesReceives salesReceives;

	private SalesReceivesRegist salesReceivesRegist;

	private Returns returns;

	private StoreFlit storeflit;
	private StoreOut storeOut;
	private CodeService codeService;
	private StoreIn storein;
	private PaginationSupport pagination;
	private Integer[] codeIds;
	private Collection<StoreInDetail> slist;
	@Autowired
	private StoreInService storeInService;
	private List arrylist;

	private BaseEntity entity;

	private String codePlan;

	private File upload;
	private String uploadFileName;
    
	public DocType getDocType() {
		return docType;
	}

	public void setDocType(DocType docType) {
		this.docType = docType;
	}

	public DeptDocType getDeptDocType() {
		return deptDocType;
	}

	public void setDeptDocType(DeptDocType deptDocType) {
		this.deptDocType = deptDocType;
	}

	public Purchase getPurchase() {
		return purchase;
	}

	public void setPurchase(Purchase purchase) {
		this.purchase = purchase;
	}

	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

	public PaymentDetails getPaymentDetail() {
		return paymentDetail;
	}

	public void setPaymentDetail(PaymentDetails paymentDetail) {
		this.paymentDetail = paymentDetail;
	}

	public PurchaseReturned getPurchaseReturned() {
		return purchaseReturned;
	}

	public void setPurchaseReturned(PurchaseReturned purchaseReturned) {
		this.purchaseReturned = purchaseReturned;
	}

	public SalesContract getSalesContract() {
		return salesContract;
	}

	public void setSalesContract(SalesContract salesContract) {
		this.salesContract = salesContract;
	}

	public QuotationSheet getQuotationsheet() {
		return quotationsheet;
	}

	public void setQuotationsheet(QuotationSheet quotationsheet) {
		this.quotationsheet = quotationsheet;
	}

	public SalesReceives getSalesReceives() {
		return salesReceives;
	}

	public void setSalesReceives(SalesReceives salesReceives) {
		this.salesReceives = salesReceives;
	}

	public SalesReceivesRegist getSalesReceivesRegist() {
		return salesReceivesRegist;
	}

	public void setSalesReceivesRegist(SalesReceivesRegist salesReceivesRegist) {
		this.salesReceivesRegist = salesReceivesRegist;
	}

	public Returns getReturns() {
		return returns;
	}

	public void setReturns(Returns returns) {
		this.returns = returns;
	}

	public StoreFlit getStoreflit() {
		return storeflit;
	}

	public void setStoreflit(StoreFlit storeflit) {
		this.storeflit = storeflit;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public StoreRoom getRoom() {
		return room;
	}

	public void setRoom(StoreRoom room) {
		this.room = room;
	}

	public Currency getCurrency() {
		return currency;
	}

	public void setCurrency(Currency currency) {
		this.currency = currency;
	}

	public District getDistrict() {
		return district;
	}

	public void setDistrict(District district) {
		this.district = district;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Salary getSalary() {
		return salary;
	}

	public void setSalary(Salary salary) {
		this.salary = salary;
	}

	public Train getTrain() {
		return train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public JobTitle getJobTitle() {
		return jobTitle;
	}

	public void setJobTitle(JobTitle jobTitle) {
		this.jobTitle = jobTitle;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Rival getRival() {
		return rival;
	}

	public void setRival(Rival rival) {
		this.rival = rival;
	}

	public DeptDoc getDeptDoc() {
		return deptDoc;
	}

	public void setDeptDoc(DeptDoc deptDoc) {
		this.deptDoc = deptDoc;
	}

	public UserDoc getUserDoc() {
		return userDoc;
	}

	public void setUserDoc(UserDoc userDoc) {
		this.userDoc = userDoc;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	@Action("editCode1")
	public String editCode1() {

		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveCode1", results = { @Result(name = "success", type = "redirect", location = "editCode1") })
	public String saveCode1() {
		this.getCodeService().save(code);

		return SUCCESS;
	}

	@Action("listCodeAll")
	public String listCode1() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Code.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCodeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "removeCode1", results = { @Result(name = "success", type = "redirect", location = "listCode1") })
	public String removeCode1() {
		this.getCodeService().remove(code);
		return SUCCESS;
	}

	@Action("ListAllCode")
	public String ListAllCode() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Code.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCodeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "redirectCodeDomain", results = {
			@Result(name = "storein", type = "redirect", location = "/stock/viewStoreIn?storein=${storein.id}"),
			@Result(name = "storeout", type = "redirect", location = "/stock/viewStoreOut?storeout=${storeOut.id}"),
			@Result(name = "supplier", type = "redirect", location = "/stock/purchase/viewSupplier?supplier=${supplier.id}"),
			@Result(name = "deptDoc", type = "redirect", location = "/doc/viewDeptDoc?deptDoc=${deptDoc.id}"),
			@Result(name = "userDoc", type = "redirect", location = "/doc/viewUserDoc?userDoc=${userDoc.id}"),
			@Result(name = "customer", type = "redirect", location = "/crm/viewCustomer?customer=${customer.id}"),
			@Result(name = "rival", type = "redirect", location = "/crm/viewRival?rival=${rival.id}"),
			@Result(name = "person", type = "redirect", location = "/hr/viewPerson?person=${person.id}"),
			@Result(name = "salary", type = "redirect", location = "/hr/viewSalary?salary=${salary.id}"),
			@Result(name = "train", type = "redirect", location = "/hr/viewTrain?train=${train.id}"),
			@Result(name = "role", type = "redirect", location = "/hr/viewRole?role=${role.id}"),
			@Result(name = "jobTitle", type = "redirect", location = "/hr/viewJobTitle?jobTitle=${jobTitle.id}"),
			@Result(name = "dept", type = "redirect", location = "/hr/viewDept?dept=${dept.id}"),
			@Result(name = "currency", type = "redirect", location = "/base/viewCurrency?currency=${currency.id}"),
			@Result(name = "district", type = "redirect", location = "/base/viewDistrict?district=${district.id}"),
			@Result(name = "product", type = "redirect", location = "/stock/viewProduct?product=${product.id}"),
			@Result(name = "room", type = "redirect", location = "/stock/viewStoreRoom?room=${room.id}"),
			@Result(name = "docType", type = "redirect", location = "/doc/viewDocType?docType=${docType.id}"),
			@Result(name = "deptDocType", type = "redirect", location = "/doc/viewDeptDocType?deptDocType=${deptDocType.id}"),
			@Result(name = "document", type = "redirect", location = "/doc/viewDocument?document=${document.id}"),			
			@Result(name = "purchase", type = "redirect", location = "/stock/viewPurchase?purchase=${purchase.id}"),
			@Result(name = "purchaseOrder", type = "redirect", location = "/purchase/viewPurchaseOrder?purchaseOrder=${purchaseOrder.id}"),
			@Result(name = "paymentDetail", type = "redirect", location = "/purchase/viewPaymentDetail?paymentDetail=${paymentDetail.id}"),
			@Result(name = "purchaseReturned", type = "redirect", location = "/purchase/viewPurchaseReturned?purchaseReturned=${purchaseReturned.id}"),
			@Result(name = "salesContract", type = "redirect", location = "/sale/viewSalesContract?salesContract=${salesContract.id}"),
			@Result(name = "quotationsheet", type = "redirect", location = "/sale/viewQuotationSheet?quotationsheet=${quotationsheet.id}"),
			@Result(name = "salesReceives", type = "redirect", location = "/sale/viewSalesReceives?salesReceives=${salesReceives.id}"),
			@Result(name = "salesReceivesRegist", type = "redirect", location = "/sale/viewSalesReceivesBill?salesReceivesRegist=${salesReceivesRegist.id}"),
			@Result(name = "returns", type = "redirect", location = "/sale/viewReturns?returns=${returns.id}"),
			@Result(name = "storeflit", type = "redirect", location = "/stock/viewStoreFlit?storeflit=${storeflit.id}")
			
	})
			
	public String redirectCodeDomain() {
		entity = this.codeService.findByCode(code);
		if (entity instanceof StoreIn) {
			storein = (StoreIn) entity;
			return "storein";
		} else if (entity instanceof StoreOut) {
			storeOut = (StoreOut) entity;
			return "storeout";
		} else if (entity instanceof Purchase) {
			purchase = (Purchase) entity;
			return "purchase";
		}

		else if (entity instanceof PurchaseOrder) {
			purchaseOrder = (PurchaseOrder) entity;
			return "purchaseOrder";
		}

		else if (entity instanceof PaymentDetails) {
			paymentDetail = (PaymentDetails) entity;
			return "paymentDetail";
		}

		else if (entity instanceof PurchaseReturned) {
			purchaseReturned = (PurchaseReturned) entity;
			return "purchaseReturned";
		}

		else if (entity instanceof SalesContract) {
			salesContract = (SalesContract) entity;
			return "salesContract";
		}

		else if (entity instanceof QuotationSheet) {
			quotationsheet = (QuotationSheet) entity;
			return "quotationsheet";
		}

		else if (entity instanceof SalesReceives) {
			salesReceives = (SalesReceives) entity;
			return "salesReceives";
		}

		else if (entity instanceof SalesReceivesRegist) {
			salesReceivesRegist = (SalesReceivesRegist) entity;
			return "salesReceivesRegist";
		}

		else if (entity instanceof Returns) {
			returns = (Returns) entity;
			return "returns";
		}

		else if (entity instanceof Supplier) {
			supplier = (Supplier) entity;
			return "supplier";

		} else if (entity instanceof DeptDoc) {
			deptDoc = (DeptDoc) entity;
			return "deptDoc";

		} else if (entity instanceof UserDoc) {
			userDoc = (UserDoc) entity;
			return "userDoc";
		} else if (entity instanceof Customer) {
			customer = (Customer) entity;
			return "customer";
		} else if (entity instanceof Rival) {
			rival = (Rival) entity;
			return "rival";
		} else if (entity instanceof Person) {
			person = (Person) entity;
			return "person";
		} else if (entity instanceof Role) {
			role = (Role) entity;
			return "role";
		} else if (entity instanceof JobTitle) {
			jobTitle = (JobTitle) entity;
			return "jobTitle";
		} else if (entity instanceof Salary) {
			salary = (Salary) entity;
			return "salary";
		} else if (entity instanceof Dept) {
			dept = (Dept) entity;
			return "dept";
		} else if (entity instanceof Train) {
			train = (Train) entity;
			return "train";
		} else if (entity instanceof Currency) {
			currency = (Currency) entity;
			return "currency";
		} else if (entity instanceof District) {
			district = (District) entity;
			return "district";
		} else if (entity instanceof Product) {
			product = (Product) entity;
			return "product";
		} else if (entity instanceof StoreRoom) {
			room = (StoreRoom) entity;
			return "room";
		}else if (entity instanceof DocType) {
			docType= (DocType) entity;
			return "docType";
		}else if (entity instanceof DeptDocType) {
			deptDocType= (DeptDocType) entity;
			return "deptDocType";
		}else if (entity instanceof Document) {
			document= (Document) entity;
			return "document";
		}
		return SUCCESS;
	}

	@Action("viewStoreIn")
	public String viewStoreIn() {
		storein = this.getStoreInService().get(storein.getId());
		this.slist = storein.getDetails();
		return SUCCESS;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Code getCode() {
		return code;
	}

	public BaseEntity getEntity() {
		return entity;
	}

	public void setEntity(BaseEntity entity) {
		this.entity = entity;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getCodeIds() {
		return codeIds;
	}

	public void setCodeIds(Integer[] codeIds) {
		this.codeIds = codeIds;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public StoreIn getStorein() {
		return storein;
	}

	public void setStorein(StoreIn storein) {
		this.storein = storein;
	}

	public Collection<StoreInDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<StoreInDetail> slist) {
		this.slist = slist;
	}

	public StoreInService getStoreInService() {
		return storeInService;
	}

	public void setStoreInService(StoreInService storeInService) {
		this.storeInService = storeInService;
	}

	public List getArrylist() {
		return arrylist;
	}

	public void setArrylist(List arrylist) {
		this.arrylist = arrylist;
	}

	public String getCodePlan() {
		return codePlan;
	}

	public void setCodePlan(String codePlan) {
		this.codePlan = codePlan;
	}

	public StoreOut getStoreOut() {
		return storeOut;
	}

	public void setStoreOut(StoreOut storeOut) {
		this.storeOut = storeOut;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	
}
